package com.cxsw.rms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cxsw.rms.entity.SysUser;
import com.cxsw.rms.mappers.StudentMapper;
import com.cxsw.rms.mappers.SysUserMapper;
import com.cxsw.rms.service.SysUserService;
import com.cxsw.rms.utils.ResultObject;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 关于系统用户业务逻辑层方法实现类
 *
 * @author Han Fuhao
 * @date 2023/05/14
 */
@Service
@PropertySource("classpath:upload.properties")
public class SysUserServiceImpl implements SysUserService {
    /**
     * 关于系统用户数据访问层Spring注入
     */
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 关于学生数据访问层的Spring注入
     */
    @Resource
    private StudentMapper studentMapper;

    /**
     * 查询登录信息并返回查询结果
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link String}
     */
    @Override
    public ResultObject selectForLogin(String username, String password) {
        SysUser sysUser = sysUserMapper.selectByUsername(username);
        if (sysUser != null) {
            if (sysUser.getPassword().equals(password)) {
                return ResultObject.success("登录成功！", sysUser);
            } else {
                return ResultObject.failure("用户名或密码错误！");
            }
        }
        return ResultObject.failure("用户不存在！");
    }

    /**
     * 用户注册
     *
     * @param sysUser 系统用户
     * @return {@link ResultObject}
     */
    @Override
    public ResultObject register(SysUser sysUser) {
        System.out.println(sysUser);
        if (StrUtil.isBlank(sysUser.getUsername())) {
            return ResultObject.failure("用户名不能为空");
        } else if (StrUtil.isBlank(sysUser.getName())) {
            return ResultObject.failure("姓名不能为空");
        } else if (StrUtil.isBlank(sysUser.getPassword())) {
            return ResultObject.failure("密码不能为空");
        } else if (StrUtil.isBlank(sysUser.getSex())) {
            return ResultObject.failure("性别不能为空");
        } else {
            if (ObjectUtil.isEmpty(sysUserMapper.selectByUsername(sysUser.getUsername()))) {
                sysUserMapper.insertUser(sysUser);
                return ResultObject.success("注册成功！", null);
            }
            return ResultObject.failure("账号已被注册");
        }
    }
}